﻿using ClosedXML.Excel;
using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Production_Modules;
using MES.Server.API.Application.Command.Production_Modules.GroupInventory;
using MES.Server.API.Application.Command.Production_Modules.MaterialOutAsk;
using MES.Server.API.Application.Command.Production_Modules.Pulldown;
using MES.Server.API.Application.Command.Production_Modules.PurchaseOrder;
using MES.Server.API.Dto.Material_Management.OutPut;
using MES.Server.API.Dto.Warehouse;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ReturnValue;
using System.Diagnostics;
using System.Net.Http;

namespace MES.Server.API.Controllers
{
    /// <summary>
    /// 控制器-仓库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductionModulesController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        IHttpClientFactory httpClientFactory;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public ProductionModulesController(IMediator mediator, IHttpClientFactory httpClientFactory)
        {
            this.mediator = mediator;
            this.httpClientFactory = httpClientFactory;
        }
        /// <summary>
        /// 物料库存添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddInventory([FromBody] InventoryCreateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 物料库存修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateInventory([FromBody] InventoryUpdateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 物料库存删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteInventory([FromBody] InventoryDeleteCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 物料库存查询
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<WarehouseDTO>>> GetInventory([FromQuery] InventoryQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 添加出库的同时修改产品物料的数量,物料请求的状态,和物料库存的数量
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMaterialOut(AddMaterialOutCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 物料入库
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMaterialIn(AddMaterialInCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 出入库记录表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<GetMaterialsOutInDTO>> GetMaterialsOutIn([FromQuery] GetMaterialsOutInCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 仓库查询
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<WarehouseModel>>> GetWare([FromQuery] WarehouseQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 仓库添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddWare([FromBody] WarehouseCreateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 仓库修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateWare([FromBody] WarehouseUpdateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 仓库删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteWare([FromBody] WarehouseDeleteCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 物料二级联动
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<GetProductMaterialsOutPut>>> GetMaterials([FromQuery] GetMaterialsCommand command)
        {
            return mediator.Send(command);

        }
        /// <summary>
        /// 产品订单管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<ProduceDTO>> GetProducetion([FromQuery] ProduceQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 生产订单添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPro([FromBody] ProduceCreateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 生产订单删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeletePro([FromBody] ProduceDeleteCommand command)
        {
            return await mediator.Send(command);
        }
        //public IActionResult PuEepStudent()
        //{
        //    using HttpClient client = new HttpClient();
        //    client.BaseAddress = new Uri("");
        //    var response= client.GetAsync("").Result;
        //    if(response.IsSuccessStatusCode)
        //    {
        //        var result=response.Content.ReadAsStringAsync().Result;
        //        JsonContent.Des
        //    }
        //}
        /// <summary>
        /// 群组入库
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddGroupIn(AddGroupInCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 群组出库
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddGroupOut(AddGroupOutCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 群组添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddGroupInventory(AddGroupInventoryCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 群组库存列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<GroupInventoryOutPut>> GetGroupInventory([FromQuery] GetGroupInventoryCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 群组出库入库记录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<GetGroupOutInOutPut>> GetGroupOutIn([FromQuery]GetGroupOutInCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 生产订单批量删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<int>> BatchDelProductions([FromQuery]BatchDelProductionCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 生产订单修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> ProduceUpdate([FromBody]ProduceUpdateCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 导出订单信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportOrder()
        {
            // 获取所有生产工单数据
            var command = new ProduceQueryCommand();
            var produceData = await mediator.Send(command);

            using var workbook = new XLWorkbook();
            var worksheet = workbook.Worksheets.Add("生产工单信息");

            // 设置表头
            worksheet.Cell(1, 1).Value = "生产编号";
            worksheet.Cell(1, 2).Value = "生产工单名称";
            worksheet.Cell(1, 3).Value = "生产类型";
            worksheet.Cell(1, 4).Value = "客户ID";
            worksheet.Cell(1, 5).Value = "订单ID";
            worksheet.Cell(1, 6).Value = "物料群组ID";
            worksheet.Cell(1, 7).Value = "仓库ID";
            worksheet.Cell(1, 8).Value = "工艺ID";
            worksheet.Cell(1, 9).Value = "车间ID";
            worksheet.Cell(1, 10).Value = "产线ID";
            worksheet.Cell(1, 11).Value = "开始时间";
            worksheet.Cell(1, 12).Value = "结束时间";
            worksheet.Cell(1, 13).Value = "生产数量";
            worksheet.Cell(1, 14).Value = "已生产数量";
            worksheet.Cell(1, 15).Value = "检查状态";

            // 填充数据
            var rowIndex = 2;
            foreach (var item in produceData.TotalPage)
            {
                worksheet.Cell(rowIndex, 1).Value = item.ProduceCode;
                worksheet.Cell(rowIndex, 2).Value = item.ProduceName;
                worksheet.Cell(rowIndex, 3).Value = item.ProduceType;
                worksheet.Cell(rowIndex, 4).Value = item.ClientId;
                worksheet.Cell(rowIndex, 5).Value = item.OrderId;
                worksheet.Cell(rowIndex, 6).Value = item.MaterialGroupId;
                worksheet.Cell(rowIndex, 7).Value = item.WarehouseId;
                worksheet.Cell(rowIndex, 8).Value = item.ProcessFlowId;
                worksheet.Cell(rowIndex, 9).Value = item.WorkShopId;
                worksheet.Cell(rowIndex, 10).Value = item.ProductionLineId;
                worksheet.Cell(rowIndex, 11).Value = item.StartDate;
                worksheet.Cell(rowIndex, 12).Value = item.EntDate;
                worksheet.Cell(rowIndex, 13).Value = item.ProduceNum;
                worksheet.Cell(rowIndex, 14).Value = item.FinishProduceNum;
                worksheet.Cell(rowIndex, 15).Value = item.Ischeck;
                rowIndex++;
            }

            // 调整列宽
            worksheet.Columns().AdjustToContents();

            // 设置响应头
            var stream = new MemoryStream();
            workbook.SaveAs(stream);
            var content = stream.ToArray();

            return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "生产工单信息.xlsx");
        }
        [HttpGet]
        public async Task<IActionResult> ReadERP()
        {
            using HttpClient client = httpClientFactory.CreateClient();
            client.BaseAddress = new Uri("http://localhost:5086/");
            var response = client.GetAsync("/api/Managemnet/ShowOrder").Result;
            response.EnsureSuccessStatusCode();
            string responseData = await response.Content.ReadAsStringAsync();
            return Ok(responseData);
        }
        /// <summary>
        /// 物料采购单发布
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPurchaseOrder(AddPurchaseOrderCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 物料采购单列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<GetPurchaseOrderOutPut>> GetPurchaseOrder([FromQuery]GetPurchaseOrderCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 供应商下拉
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<GetVendorOutPut>> GetVendor([FromQuery]GetVendorCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 物料入库申请单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMaterialOutAsk(AddMaterialOutAskCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 物料入库申请单列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<GetMaterialOutAskOutPut>> GetMaterialOutAsk([FromQuery]GetMaterialOutAskCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request, cancellationToken);
        }
    }
    
}
